package pers.li.dao;

import pers.li.model.User;
import pers.li.model.VO.UserVO;
import pers.li.utils.DataProcess;

public class UserDao {
    private final DataProcess dp = new DataProcess(true);

    public User getUserByUserName(String userName) {
        String sql = "select *from t_user where user_name = ?";
        return dp.getObject(sql, User.class, userName);
    }

    public int insertUser(User user) {
        String sql = "insert into t_user (user_name, user_pass, user_age, user_sex, user_email, user_rank)\n" +
                "values (?,?,?,?,?,?)";
        return dp.insertRow(sql, user.getUserName(), user.getUserPass(), user.getUserAge(),
                user.getUserSex(), user.getUserEmail(), user.getUserRank());
    }

    public User getUserById(Integer id) {
        String sql = "select *from t_user where user_id = ?";
        return dp.getObject(sql, User.class, id);
    }

    public int updateUser(UserVO userVO) {
        String sql = "update t_user\n" +
                "set user_age = ?,user_email = ?,user_sex = ?\n" +
                "where user_id = ?";
        return dp.updateRow(sql, userVO.getUserAge(), userVO.getUserEmail(),
                userVO.getUserSex(), userVO.getUserId());
    }

    public int updatePassword(String password,Integer userId){
        String sql = "update t_user\n" +
                "set user_pass = ?\n" +
                "where user_id = ?;";
        return dp.updateRow(sql,password,userId);
    }

}
